package com.weipai.demo.service.impl;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.UserMapper;
import com.weipai.demo.mapper.base.DUserMapper;
import com.weipai.demo.model.DUser;
import com.weipai.demo.service.UserService;
import com.weipai.demo.util.Response;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author songmengqiang
 * @create 2018-08-29 19:00
 **/
@Service
public class UserServiceImpl implements UserService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private static int TIME_OUT = 3600 * 24 * 30;
    @Autowired
    private DUserMapper dUserMapper;
    @Autowired
    private UserMapper userMapper;



    @Override
    public DUser getUserByLoginName(String userName) {
        return null;
    }

    @Override
    public DUser getUserByLoginUserId(String userId) {
        return null;
    }

    //登录
    @Override
    public Response login(DUser dUser) {
        Response response = new Response();
        List<DUser> users = dUserMapper.select(dUser);
        if (users.size()!=0){
            response.put("users",users);
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.PASSWORDERROR);
            return response;
        }


        return response;
    }


    //退出
    @Override
    public Response loginOut() {
        Response response = new Response();
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
       /* User user = (User) subject.getSession().getAttribute(User.KEY);*/
       /* logger.info("user {}", user);*/
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    /**
     *
     * 修改密码
     * @author gusl
     * @date 2019/5/31 17:41
     * @param  [userId, oldPassword, newPassword, twoPassword]
     * @return com.weipai.springboot.model.api.Response
     */

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Response resetUserPassword(String userName,String oldPassword, String newPassword, String twoPassword,Integer id) {
        Response response = new Response();
        //判断两次密码是否一样

        try {
            if (!newPassword.equals(twoPassword)) {
                response.setCode(RetCode.PASSWORD_NOT_SAME);
                return response;
            } else {
                DUser dUser=new DUser();
                dUser.setUserName(userName);
                dUser.setUserPassword(newPassword);
                dUser.setId(id);
                dUserMapper.updateByPrimaryKeySelective(dUser);
               response.setCode(RetCode.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }


        return response;
    }



}
